package com.zh.mycommon.value;

import org.jetbrains.annotations.NotNull;

/**
 * @author Zhou
 * @date 19:08 2025/6/1
 */

public sealed interface ID extends Comparable<ID> {

    Object value();

    record _StringId(String value) implements ID {

        @Override
        public int compareTo(@NotNull ID other) {
            if (other instanceof _StringId stringId) {
                return value.compareTo(stringId.value());
            } else if (other instanceof _IntId intId) {
                return value.compareTo(Integer.toString(intId.value()));
            } else if (other instanceof _LongId longId) {
                return value.compareTo(Long.toString(longId.value()));
            }
            throw new IllegalArgumentException("Unexpected type: " + other.getClass());
        }

    }

    record _IntId(Integer value) implements ID {
        @Override
        public int compareTo(@NotNull ID other) {
            if (other instanceof _StringId stringId) {
                return Integer.toString(value).compareTo(stringId.value());
            } else if (other instanceof _IntId intId) {
                return Integer.compare(value, intId.value());
            } else if (other instanceof _LongId longId) {
                return Long.compare(value, longId.value());
            }
            throw new IllegalArgumentException("Unexpected type: " + other.getClass());
        }
    }

    record _LongId(Long value) implements ID {
        @Override
        public int compareTo(@NotNull ID other) {
            if (other instanceof _StringId stringId) {
                return Long.toString(value).compareTo(stringId.value());
            } else if (other instanceof _IntId intId) {
                return Long.compare(value, intId.value());
            } else if (other instanceof _LongId longId) {
                return Long.compare(value, longId.value());
            }
            throw new IllegalArgumentException("Unexpected type: " + other.getClass());
        }
    }

    // 静态工厂方法替代顶层函数
    static ID StringId(String value) {
        return new _StringId(value);
    }

    static ID IntId(int value) {
        return new _IntId(value);
    }

    static ID LongId(long value) {
        return new _LongId(value);
    }
}
